
const g = require('../utils/global');

// 损伤格子数据
class CellModel {
    constructor() {
        this.type = null;
        this.status = g.CELL_STATUS.common;
        // 初始格子
        this.x = 1;
        this.y = 1;
        this.startX = 1;
        this.startY = 1;
        // 触发鸟消除的图标
        this.birdType = null;
        // 界面显示
        this.cmd = [];
        this.isDeath = false;
    }

    init(cellType) {
        this.type = cellType;
    }

    setEmpty() {
        this.type = g.CELL_TYPE.empty;
    }

    setXY(x, y) {
        this.x = x;
        this.y = y;
    }

    setStartXY(x, y) {
        this.startX = x;
        this.startY = y;
    }

    setStatus(cellStatus) {
        this.status = cellStatus;
    }
    // 触发鸟消除的图标
    setBirdType(cellType) {
        if (this.status === g.CELL_STATUS.bird) {
            this.birdType = cellType;
        }
    }

    // 移动然后返回
    moveToAndBack(pos) {
        const srcPos = cc.v2(this.x, this.y);
        this.cmd.push({
            action: g.CELL_ACTION.moveTo,
            playTime: 0,
            keepTime: g.ANI_TIME.move,
            pos: pos,
        });
        this.cmd.push({
            action: g.CELL_ACTION.moveTo,
            playTime: g.ANI_TIME.move,
            keepTime: g.ANI_TIME.move,
            pos: srcPos,
        });
    }

    moveTo(pos, playTime) {
        this.cmd.push({
            action: g.CELL_ACTION.moveTo,
            playTime: playTime,
            keepTime: g.ANI_TIME.move,
            pos: pos
        });
        this.x = pos.x;
        this.y = pos.y;
    }

    // 摇摆
    toShake(playTime) {
        this.cmd.push({
            action: g.CELL_ACTION.toShake,
            playTime: playTime,
            keepTime: g.ANI_TIME.die_shake,
        });
    }

    // 死亡，消除 cell
    toDie(playTime) {
        this.cmd.push({
            action: g.CELL_ACTION.toDie,
            playTime: playTime,
            keepTime: g.ANI_TIME.die,
        });
        this.isDeath = true;
    }

    // 设置可见
    setVisible(playTime, isVisible) {
        this.cmd.push({
            action: g.CELL_ACTION.setVisible,
            playTime: playTime,
            keepTime: 0,
            isVisible: isVisible
        });
    }

    cleanCmd() {
        this.cmd = [];
    }
}

module.exports = {
    CellModel,
};